/*
 * QTXpost - a Qt-based blog client.
 * Copyright (C) 2010 Helen Sommer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 *
 * The project is hosted on http://code.google.com/p/qtxpost
 */

#include "SelectPost.h"
#include "Server.h"
#include "MainWindow.h"

extern MainWindow *root;

SelectPost::SelectPost(QWidget *parent) 
  : QDialog(parent)
{
  setupUi(this);
  connect(selectButton, SIGNAL(clicked()), this, SLOT(select()));
  connect(closeButton, SIGNAL(clicked()), this, SLOT(close()));
  connect(moreButton, SIGNAL(clicked()), this, SLOT(more()));
  connect(dateButton, SIGNAL(clicked()), this, SLOT(dateUpdate()));
  connect(dateLine, SIGNAL(returnPressed()), this, SLOT(dateUpdate()));

  layout = new QGridLayout(scrollArea);
  layout->setColumnStretch(3, 2); // 3rd column (subject) should stretch, other columns - not
  buttonGroup = new QButtonGroup(this);

  font = scrollArea->font();
  font.setBold(true);

  lockedPixmap = QPixmap(":/images/locked.png");
  filteredPixmap = QPixmap(":/images/filtered.png");
  privatePixmap = QPixmap(":/images/private.png");
}

void SelectPost::initialize(Server *server_)
{
  progressBar->setValue(0);
  cleanup();
  initializeProgress(20);
  server = server_;
  lastDate = "";

  show();
}

void SelectPost::initializeProgress()
{
  progressBar->show();
  progressBar->setValue(0);
  
  setCursor(Qt::WaitCursor);
}

void SelectPost::initializeProgress(int target_)
{
  currentCursor = cursor();
  target = target_;
  count = 0;
  step = 100/target;
  initializeProgress();
}

void SelectPost::incrementProgress()
{
  count++;
  progressBar->setValue(progressBar->value() + step);
  if (count >= target) 
    completeProgress();
}

void SelectPost::completeProgress()
{
  progressBar->setValue(100);
  setCursor(currentCursor);
}

void SelectPost::add(int itemid, QString subject, QString eventtime, QString security, qulonglong allowmask, QString event, QString url, QString poster)
{
  QRadioButton *selectButton = new QRadioButton();
  QLabel *dateLabel = new QLabel(eventtime);
  QLabel *subjectLabel = new QLabel((subject == "") ? QString(tr("No subject")) : subject);
  subjectLabel->setFont(font);
  QLabel *eventLabel = new QLabel(event);

  QString urlString = url;
  if (poster != "") {
    urlString.append(" [ ").append(poster).append(" ] ");
  }
  QLabel *urlLabel = new QLabel(urlString);

  QLabel *securityLabel = new QLabel();

  if (security == "usemask") {
    if (allowmask == 1)
      securityLabel->setPixmap(lockedPixmap);
    else
      securityLabel->setPixmap(filteredPixmap);
  }
  else if (security == "private") {
    securityLabel->setPixmap(privatePixmap);
  }

  buttonGroup->addButton(selectButton);
  buttonGroup->setId(selectButton, itemid);
  
  int row = layout->rowCount();
  layout->addWidget(selectButton, row, 0);
  layout->addWidget(dateLabel, row, 1);
  layout->addWidget(securityLabel, row, 2);
  layout->addWidget(urlLabel, row, 3, Qt::AlignLeft);
  layout->addWidget(subjectLabel, row + 1, 1, 1, 4, Qt::AlignLeft);
  layout->addWidget(eventLabel, row + 2, 1, 1, 4, Qt::AlignLeft);

  incrementProgress();
  
  if (eventtime != "") { // shouldn't happen unless this entry is corrupted on the server
    lastDate = eventtime;
  }
}

void SelectPost::select()
{
  int itemid = buttonGroup->checkedId();
  if (itemid == -1) return;
  server->itemid = itemid;
  root->editPost(server);
}

void SelectPost::more()
{
  server->startGetListPosts();
}

void SelectPost::dateUpdate()
{
  QMessageBox msgBox;

  msgBox.setIcon(QMessageBox::Warning);
  msgBox.setInformativeText(tr("Format of date: yyyy-mm-dd"));
    
  QString str = dateLine->text().simplified();
  if (str == "") {
    msgBox.setText(tr("Please enter date!"));
    msgBox.exec();
    return;
  }
  else {
    QDateTime date = QDateTime::fromString(str, "yyyy-MM-dd");
    if (!date.isValid()) {
      msgBox.setText(tr("Invalid date!"));
      msgBox.exec();
      return;
    }
  }
  
  lastDate = str.append(" 00:00:00");
  cleanup();
  server->startGetListPosts();
}

void SelectPost::errorMessage(QString message)
{
  QLabel *label = new QLabel(message);
  layout->addWidget(label, 0, 1, 1, 4, Qt::AlignLeft|Qt::AlignTop);
  
}

void SelectPost::cleanup()
{
  for (int i=0; i < layout->count(); i++) {
    layout->itemAt(i)->widget()->hide();
  }
}
